using System.Collections;
using Bullet;
using UnityEngine;
using UnityEngine.AI; 
namespace Enemy
{
    public class Bee : BaseEnemy
    {
        private float shutTime = 0.5f;//攻击的开始时间

        private  NavMeshAgent nav; //获取导航网格代理组件，通过此组件来告知AI目标
        private Vector3 target;//获取目标
        //访问父类属性
        private void Start()
        {
            Hp = MaxHp;
            nav = GetComponent<NavMeshAgent>();
            bulletPrefab = Resources.Load<GameObject>("Prefabs/Enemy/Bullet/EnemyBullet2");
            point = transform.Find("point").gameObject;
            base.Start();
        }

        private void Update()
        {
            //Action();
            base.Update();
        }

        public override void Action()
        {
            if(isDead)
            {
                //导航代理停止移动
                nav.isStopped = true;
                return;
            }
            //向玩家移动
            if (Player.Player.instance != null)
            {
                if (Time.time >= nextAttackTime)
                {
                    transform.LookAt(Player.Player.instance.transform);

                    //位置可以偏移一点
                    target = Player.Player.instance.transform.position + new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));
                    nav.SetDestination(Player.Player.instance.transform.position); //每帧更新目标位置

                    StartCoroutine(CreateBullet());
                    nextAttackTime = Time.time + attackCooldown;
                }

            }

        }

        /// <summary>
        /// 创建导弹
        /// </summary>
        /// <returns></returns>
        IEnumerator CreateBullet()
        {
            /*//创建一个导弹
            GameObject bullet = Instantiate(bulletPrefab);
            //bullet的旋转和位置此脚本对象一致
            bullet.transform.rotation = transform.rotation;
            bullet.transform.position = transform.position;
            bullet.GetComponent<EnemyBullet>().AttackRange = AttackRange;
            yield return new WaitForSeconds(1f);*/
            
            
            //利用对象池来管理敌人子弹
            //GameObject bullet = PoolManager.Release(bulletPrefab, transform.position, transform.rotation);
            GameObject bullet = PoolManager.Release(bulletPrefab, point.transform.position, point.transform.rotation);
            bullet.GetComponent<EnemyBullet>().attackRange = AttackRange;
            yield return new WaitForSeconds(1f);
        }
        
        public void OnTriggerEnter(Collider other)
        {
            base.OnTriggerEnter(other);

            if (other.gameObject.tag == "Bullet")
            {
                GetHit();

            }
        }
    }
}
